package org.example.unfiar;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;

public class MapTest {         // java.util.ConcurrentModificationException 并发修改异常
    // 测试Map线程安全性
    public static void main(String[] args) {

        // 解决方法：
        // 1. Map<String, String> map = Collections.synchronizedMap(oldMap); 工具类
        // 2. Map<String, String> map = new ConcurrentHashMap<>();           JUC 调用 Lock

        Map<String, String> map = new ConcurrentHashMap<>();

        for (int i = 1; i <= 30; i++) {
            new Thread(() -> {
                map.put(Thread.currentThread().getName(), UUID.randomUUID().toString().substring(0, 5));
                System.out.println(map);
            }, String.valueOf(i)).start();
        }
    }
}
